1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! This is the Module to interact in a save way with the Arduino Serial C++ library.
//!
//! You will need to uncomment the Serial in the config.toml file.
use crate::arduino_system::progmem::Pstring;
use core::ffi::{c_char, c_int, c_long, c_size_t, c_uchar, c_uint, c_ulong};

use crate::libraries::arduboy2_library::print::Base;
extern "C" {
    #[link_name = "arduino_serial_begin"]
    fn serial_begin(serial: c_ulong);
    #[link_name = "arduino_serial_end"]
    fn serial_end();
    #[link_name = "arduino_serial_available"]
    fn serial_available() -> c_int;
    #[link_name = "arduino_serial_read"]
    fn serial_read() -> c_int;
}

///The Arduino Serial Print class is available for writing text to the screen buffer.
///
///In the same manner as the Arduino arduboy.print(), etc., functions.
///
///
///Example
/// ```
/// use arduboy_rust::prelude::*;
/// let value: i16 = 42;
///
/// serial::print(b"Hello World\n\0"[..]); // Prints "Hello World" and then sets the
///                                        // text cursor to the start of the next line
/// serial::print(f!(b"Hello World\n")); // Prints "Hello World" but does not use the 2kb ram
/// serial::print(value); // Prints "42"
/// serial::print("\n\0"); // Sets the text cursor to the start of the next line
/// serial::print("hello world") // Prints normal [&str]
/// ```
pub fn print(x: impl Serialprintable) {
    x.print()
}
///The Arduino Serial Print class is available for writing text to the screen buffer.
///
///In the same manner as the Arduino arduboy.print(), etc., functions.
///
///
///Example
/// ```
/// use arduboy_rust::prelude::*;
/// let value: i16 = 42;
///
/// serial::print(b"Hello World\n\0"[..]); // Prints "Hello World" and then sets the
///                                        // text cursor to the start of the next line
/// serial::print(f!(b"Hello World\n")); // Prints "Hello World" but does not use the 2kb ram
/// serial::print(value); // Prints "42"
/// serial::print("\n\0"); // Sets the text cursor to the start of the next line
/// serial::print("hello world") // Prints normal [&str]
/// ```
pub fn println(x: impl Serialprintlnable) {
    x.println()
}
/// Sets the data rate in bits per second (baud) for serial data transmission. For communicating with Serial Monitor, make sure to use one of the baud rates listed in the menu at the bottom right corner of its screen. You can, however, specify other rates - for example, to communicate over pins 0 and 1 with a component that requires a particular baud rate.
///
/// ### Example
/// ```
/// use arduboy_rust::prelude::*;
/// serial::begin(9600)
/// ```
pub fn begin(baud_rates: u32) {
    unsafe { serial_begin(baud_rates) }
}
/// Disables serial communication, allowing the RX and TX pins to be used for general input and output. To re-enable serial communication, call [begin()].
pub fn end() {
    unsafe { serial_end() }
}
/// Reads incoming serial data.
/// Use only inside of [available()]:
///```
/// use arduboy_rust::prelude::*;
/// if serial::available() > 0 {
///     // read the incoming byte:
///     let incoming_byte: i16 = serial::read();
///
///     // say what you got:
///     serial::print("I received: ");
///     serial::println(incoming_byte);
/// }
/// ```
/// ### Returns
///
///The first byte of incoming serial data available (or -1 if no data is available). Data type: int.
pub fn read() -> i16 {
    unsafe { serial_read() }
}
/// Reads incoming serial data.
///
/// Use only inside of [available()]:
/// ```
/// use arduboy_rust::prelude::*;
/// if serial::available() > 0 {
///     // read the incoming byte:
///     let incoming_byte: &str = serial::read_as_utf8_str();
///
///     // say what you got:
///     serial::print("I received: ");
///     serial::println(incoming_byte);
/// }
/// ```
/// ### Returns
///
///The first byte of incoming serial data available (or -1 if no data is available). Data type: &str.
pub fn read_as_utf8_str() -> &'static str {
    let incoming_byte = unsafe { serial_read() };
    static mut L: [u8; 2] = [0, 0];
    unsafe {
        L[0] = incoming_byte as u8;
    }
    unsafe { core::str::from_utf8(&L).unwrap() }
}

/// Get the number of bytes (characters) available for reading from the serial port. This is data that’s already arrived and stored in the serial receive buffer (which holds 64 bytes).
/// ### Example
/// ```
/// use arduboy_rust::prelude::*;
/// if serial::available() > 0 {
///     // read the incoming byte:
///     let incoming_byte = serial::read();
///
///     // say what you got:
///     serial::print("I received: ");
///     serial::println(incoming_byte);
/// }
/// ```
pub fn available() -> i16 {
    unsafe { serial_available() }
}
pub trait Serialprintlnable
where
    Self: Sized,
{
    type Parameters;

    fn println_2(self, params: Self::Parameters);
    fn default_parameters() -> Self::Parameters;

    fn println(self) {
        self.println_2(Self::default_parameters());
    }
}

impl Serialprintlnable for i16 {
    type Parameters = Base;

    fn println_2(self, params: Self::Parameters) {
        unsafe {
            println_int(self, params as c_int);
        }
    }

    fn default_parameters() -> Self::Parameters {
        Base::Dec
    }
}

impl Serialprintlnable for u16 {
    type Parameters = Base;

    fn println_2(self, params: Self::Parameters) {
        unsafe {
            println_unsigned_int(self, params as c_int);
        }
    }

    fn default_parameters() -> Self::Parameters {
        Base::Dec
    }
}

impl Serialprintlnable for i32 {
    type Parameters = Base;

    fn println_2(self, params: Self::Parameters) {
        unsafe {
            println_long(self, params as c_int);
        }
    }

    fn default_parameters() -> Self::Parameters {
        Base::Dec
    }
}

impl Serialprintlnable for u32 {
    type Parameters = Base;

    fn println_2(self, params: Self::Parameters) {
        unsafe {
            println_unsigned_long(self, params as c_int);
        }
    }

    fn default_parameters() -> Self::Parameters {
        Base::Dec
    }
}

impl Serialprintlnable for &[u8] {
    type Parameters = ();

    fn println_2(self, _params: Self::Parameters) {
        unsafe {
            println_chars(self as *const [u8] as *const i8);
        }
    }

    fn default_parameters() -> Self::Parameters {}
}

impl Serialprintlnable for &str {
    type Parameters = ();

    fn println_2(self, _params: Self::Parameters) {
        unsafe {
            println_chars(self.as_bytes() as *const [u8] as *const i8);
        }
    }

    fn default_parameters() -> Self::Parameters {}
}
impl<const N: usize> Serialprintlnable for heapless::String<N> {
    type Parameters = ();

    fn println_2(self, _params: Self::Parameters) {
        unsafe {
            println_chars(self.as_bytes() as *const [u8] as *const i8);
        }
    }

    fn default_parameters() -> Self::Parameters {}
}

impl Serialprintlnable for Pstring {
    type Parameters = ();

    fn println_2(self, _params: Self::Parameters) {
        unsafe {
            println_chars_progmem(self.pointer);
        }
    }

    fn default_parameters() -> Self::Parameters {}
}

extern "C" {
    #[link_name = "arduino_serial_println_chars"]
    fn println_chars(cstr: *const c_char);
    #[doc(hidden)]
    #[link_name = "arduino_serial_println_chars_progmem"]
    fn println_chars_progmem(pstring: *const c_char);
    // #[link_name = "arduino_serial_println_char"]
    // fn println_char(c: c_char) -> c_size_t;
    #[doc(hidden)]
    #[link_name = "arduino_serial_println_int"]
    fn println_int(n: c_int, base: c_int) -> c_size_t;
    #[doc(hidden)]
    #[link_name = "arduino_serial_println_long"]
    fn println_long(n: c_long, base: c_int) -> c_size_t;
    #[allow(dead_code)]
    #[doc(hidden)]
    #[link_name = "arduino_serial_println_unsigned_char"]
    fn println_unsigned_char(n: c_uchar, base: c_int) -> c_size_t;
    #[doc(hidden)]
    #[link_name = "arduino_serial_println_unsigned_int"]
    fn println_unsigned_int(n: c_uint, base: c_int) -> c_size_t;
    #[doc(hidden)]
    #[link_name = "arduino_serial_println_unsigned_long"]
    fn println_unsigned_long(n: c_ulong, base: c_int) -> c_size_t;
}

pub trait Serialprintable
where
    Self: Sized,
{
    type Parameters;

    fn print_2(self, params: Self::Parameters);
    fn default_parameters() -> Self::Parameters;

    fn print(self) {
        self.print_2(Self::default_parameters());
    }
}

impl Serialprintable for i16 {
    type Parameters = Base;

    fn print_2(self, params: Self::Parameters) {
        unsafe {
            print_int(self, params as c_int);
        }
    }

    fn default_parameters() -> Self::Parameters {
        Base::Dec
    }
}

impl Serialprintable for u16 {
    type Parameters = Base;

    fn print_2(self, params: Self::Parameters) {
        unsafe {
            print_unsigned_int(self, params as c_int);
        }
    }

    fn default_parameters() -> Self::Parameters {
        Base::Dec
    }
}

impl Serialprintable for i32 {
    type Parameters = Base;

    fn print_2(self, params: Self::Parameters) {
        unsafe {
            print_long(self, params as c_int);
        }
    }

    fn default_parameters() -> Self::Parameters {
        Base::Dec
    }
}

impl Serialprintable for u32 {
    type Parameters = Base;

    fn print_2(self, params: Self::Parameters) {
        unsafe {
            print_unsigned_long(self, params as c_int);
        }
    }

    fn default_parameters() -> Self::Parameters {
        Base::Dec
    }
}

impl Serialprintable for &[u8] {
    type Parameters = ();

    fn print_2(self, _params: Self::Parameters) {
        unsafe {
            print_chars(self as *const [u8] as *const i8);
        }
    }

    fn default_parameters() -> Self::Parameters {}
}

impl Serialprintable for &str {
    type Parameters = ();

    fn print_2(self, _params: Self::Parameters) {
        unsafe {
            print_chars(self.as_bytes() as *const [u8] as *const i8);
        }
    }

    fn default_parameters() -> Self::Parameters {}
}
impl<const N: usize> Serialprintable for heapless::String<N> {
    type Parameters = ();

    fn print_2(self, _params: Self::Parameters) {
        unsafe {
            print_chars(self.as_bytes() as *const [u8] as *const i8);
        }
    }

    fn default_parameters() -> Self::Parameters {}
}

impl Serialprintable for Pstring {
    type Parameters = ();

    fn print_2(self, _params: Self::Parameters) {
        unsafe {
            print_chars_progmem(self.pointer);
        }
    }

    fn default_parameters() -> Self::Parameters {}
}

extern "C" {
    #[link_name = "arduino_serial_print_chars"]
    fn print_chars(cstr: *const c_char);
    #[doc(hidden)]
    #[link_name = "arduino_serial_print_chars_progmem"]
    fn print_chars_progmem(pstring: *const c_char);
    // #[link_name = "arduino_serial_print_char"]
    // fn print_char(c: c_char) -> c_size_t;
    #[doc(hidden)]
    #[link_name = "arduino_serial_print_int"]
    fn print_int(n: c_int, base: c_int) -> c_size_t;
    #[doc(hidden)]
    #[link_name = "arduino_serial_print_long"]
    fn print_long(n: c_long, base: c_int) -> c_size_t;
    #[allow(dead_code)]
    #[doc(hidden)]
    #[link_name = "arduino_serial_print_unsigned_char"]
    fn print_unsigned_char(n: c_uchar, base: c_int) -> c_size_t;
    #[doc(hidden)]
    #[link_name = "arduino_serial_print_unsigned_int"]
    fn print_unsigned_int(n: c_uint, base: c_int) -> c_size_t;
    #[doc(hidden)]
    #[link_name = "arduino_serial_print_unsigned_long"]
    fn print_unsigned_long(n: c_ulong, base: c_int) -> c_size_t;
}